Array.prototype.treeForEach = function (call, children = "children") {
  const list = [];
  const queue = [...this];
  if (call) {
    for (const node of queue) {
        call(node);
    }
  }
  while (queue.length) {
    const node = queue.shift();
    const childrenData = node[children];
    if (childrenData) {
      if (call) {
        for (const chiNode of childrenData) {
            call(chiNode, node);
        }
      }
      queue.push(...childrenData);
    }
    list.push(node);
  }
  return list;
};

Array.prototype.treeForEachTop = function (topCall, childrenCall, children = "children") {
    const list = [];
    const queue = [...this];
    if (topCall) {
      for (const node of queue) {
          topCall(node);
      }
    }
    while (queue.length) {
      const node = queue.shift();
      const childrenData = node[children];
      if (childrenData) {
        if (childrenCall) {
          for (const chiNode of childrenData) {
              childrenCall(chiNode, node);
          }
        }
        queue.push(...childrenData);
      }
      list.push(node);
    }
    return list;
  };

/**
 * 树转列表
 * @param {*} tree 树数据
 * @param {*} children 子集属性名
 * @param {*} pid 扩展父节点唯一标识。（转列表时候增加扩展属性）
 * @param {*} id  扩展唯一标识。（转列表时候增加扩展属性）
 * @param {*} uuid 
 * @returns 
 */
function treeToList(tree, children = "children", pid = 'pid', id = 'id', uuid = 'uuid') {
  const list = [];
  const queue = [...tree];
  if (pid) {
    if (!id) {
      id = "id";
    }
    for (const node of queue) {
      node[uuid] = Math.random().toString(36).slice(2);
      node[pid] = 0;
    }
  }

  while (queue.length) {
    const node = queue.shift();

    node[pid];
    const childrenData = node[children];

    if (childrenData) {
      if (pid) {
        for (const no of childrenData) {
          no[uuid] = Math.random().toString(36).slice(2);
          no[pid] = node[id];
        }
      }
      queue.push(...childrenData);
    }
    list.push(node);
  }
  return list;
}

function listToTree(tree, pid = "pid", id = "id", children = "children") {
  const treeData = [];
  for (const node of tree) {
    // pid 空 或 0 为根节点
    if (!node[pid] || node[pid] === 0) {
      let p = { ...node };
      p[children] = getChildren(p[id], tree);
      treeData.push(p);
    }
  }
  function getChildren(id, list) {
    const children = [];
    for (const node of list) {
      if (node[pid] === id) {
        children.push(node);
      }
    }

    for (const node of children) {
      const children = getChildren(node[id], list);
      if (children.length) {
        node[children] = children;
      }
    }
    return children;
  }

  return treeData;
}

function filterTree2(tree = [], map = [], arr = []) {
  if (!tree.length) return [];
  for (let item of tree) {
    if (!map.includes(item.id)) continue;
    let node = { ...item, children: [] };
    arr.push(node);
    if (item.children && item.children.length)
      filterTree2(item.children, map, node.children);
  }
  return arr;
}

function getParId(list = [], value = [], res = []) {
  const fList = list.filter((item) => {
    return value.includes(item.pid);
  });
  console.log("f", fList);

  if (fList.length != 0) {
    getParId(
      list,
      fList.map((item) => item.id, res)
    );
  }
  //   res.push(fList);
}
